/*
 * EditUserAction.java
 *
 * Created on 3 marzec 2007, 12:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package arocms.signin;

import arocms.business.User;
import arocms.business.UserDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author arek
 */
public class EditUserAction extends Action{
    
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private final Logger log = Logger.getLogger(this.getClass());
    
    /** Creates a new instance of EditUserAction */
    public EditUserAction() {
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        if (id==null || id.trim().length()==0) {
            return mapping.findForward(FAILURE);
        }
        log.debug("--> execute, " + id);
        Long pk = new Long(id);
        RegisterUserForm aForm = new RegisterUserForm();
        User user = new UserDAO().find(pk);
        aForm.setBirthDate(user.getBirthDate().toString());
        aForm.setCity(user.getCity());
        aForm.setEmail(user.getEmail());
        aForm.setLogin(user.getEmail());
        aForm.setMobileno(user.getMobileno());
        aForm.setName(user.getName());
//        aForm.setPassword(user.getPassword());
        aForm.setPhoneno(user.getPhoneno());
        aForm.setSex(user.getSex());
        aForm.setStreet(user.getStreet());
        aForm.setSurname(user.getSurname());
        aForm.setZipcode(user.getZipcode());
        aForm.setId(id);
        log.debug("form id = " + aForm.getId() + " database id " + id);
        request.setAttribute(mapping.getAttribute(),aForm);
        log.debug("<-- execute");
        return mapping.findForward(SUCCESS);
    }
    
}
